package com.share.two.Pub_Sub;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Publish {
	
	public static void main(String[] args) throws Exception {
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
				"sunxj", 
				"123123", 
				"tcp://localhost:61616");
		Connection connection = connectionFactory.createConnection();
		connection.start();
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		
		Destination destination = session.createTopic("topic1");
		MessageProducer messageProducer = session.createProducer(destination);
		messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
		
		for (int i = 0; i < 10000; i++) {
			TextMessage textMessage = session.createTextMessage();
			textMessage.setText("我是message" + i);
			messageProducer.send(textMessage);
			System.out.println("生产者：" + textMessage.getText());
			Thread.sleep(3000);
		}
		
		if (connection != null) {
			connection.close();
		}
		
	}

}
